//
//  GoodTitleCell.m
//  jipin
//
//  Created by chuangjia on 19/5/15.
//  Copyright (c) 2015 liuchengbin. All rights reserved.
//

#import "GoodTitleCell.h"

@interface GoodTitleCell()

@property(nonatomic,strong)RTLabel *titleLabel;
@property(nonatomic,strong)RTLabel *contentLabel;
@property(nonatomic,strong)RTLabel *subLabel;

@end
@implementation GoodTitleCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        self.backgroundColor=[UIColor clearColor];
        _titleLabel =[[RTLabel alloc] initWithFrame:CGRectMake(PHOTO_FRAME_WIDTH,0, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*2,0)];
        _titleLabel.backgroundColor = [UIColor clearColor];
        _titleLabel.textColor = [UIColor darkGrayColor];
        _titleLabel.textAlignment = NSTextAlignmentLeft;
        _titleLabel.font = FONT(20);
        _titleLabel.lineSpacing =LSpacing;
        [self.contentView addSubview:_titleLabel];
        UITapGestureRecognizer* singleRecognizer= [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTapFromCategory)];
        _contentLabel =[[RTLabel alloc] initWithFrame:CGRectMake(PHOTO_FRAME_WIDTH,0,(DEVICE_WIDTH-PHOTO_FRAME_WIDTH)/2,0)];
        _contentLabel.backgroundColor = [UIColor clearColor];
        _contentLabel.textColor = UIColorFromRGB(0x285790);
        _contentLabel.textAlignment = NSTextAlignmentLeft;
        _contentLabel.font = FONT(16);
        _contentLabel.userInteractionEnabled =YES;
        _contentLabel.lineSpacing =LSpacing;
        [_contentLabel addGestureRecognizer:singleRecognizer];
        [self.contentView addSubview:_contentLabel];
        singleRecognizer=nil;
        
        UITapGestureRecognizer* singleRecognizer1= [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTapFromBrand)];
        _subLabel =[[RTLabel alloc] initWithFrame:CGRectMake(_contentLabel.frame.size.width+_contentLabel.frame.origin.x,0,DEVICE_WIDTH-PHOTO_FRAME_WIDTH*2,0)];
        _subLabel.backgroundColor = [UIColor clearColor];
        _subLabel.textColor = UIColorFromRGB(0x285790);
        _subLabel.textAlignment = NSTextAlignmentLeft;
        _subLabel.font = FONT(16);
        _subLabel.userInteractionEnabled =YES;
        [_subLabel addGestureRecognizer:singleRecognizer1];
        [self.contentView addSubview:_subLabel];
        singleRecognizer1=nil;
       
    }
    return self;
}
-(void)dealloc
{
    self.data=nil;
    self.key=nil;
    self.titleLabel=nil;
    self.contentLabel=nil;
    self.subLabel=nil;
    //    DLog(@"%s", __PRETTY_FUNCTION__);
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    
}
/// 根据数据模型来显示内容
- (void)showInfo:(id)model indexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dict =(NSDictionary *)model;
    self.data =model;
    self.indexPath =indexPath;
    NSString *name =[[NSString stringWithFormat:@"%@", [dict objectForKey:@"title"]] replaceNullString];
    if (name.length==0 || [name isEqualToString:@"(null)"])
    {
        name =@"";
    }
    if (name.length>0) {
        name =[NSString stringWithFormat:@"%@",name];
    }
    [_titleLabel setText:name];
    CGSize optimumSize1 = [_titleLabel optimumSize];
    
    _titleLabel.frame =CGRectMake(PHOTO_FRAME_WIDTH,14, optimumSize1.width, optimumSize1.height);
    CGFloat x =PHOTO_FRAME_WIDTH;
    NSString *cat =[[NSString stringWithFormat:@"%@", [dict objectForKey:@"cat2"]] replaceNullString];
    if (cat.length>0) {
        [_contentLabel setText:cat];
        CGSize optimumSize2 = [_contentLabel optimumSize];
        _contentLabel.frame =CGRectMake(x,_titleLabel.frame.origin.y+_titleLabel.frame.size.height+8,optimumSize2.width, optimumSize2.height);
        x =_contentLabel.frame.origin.x+_contentLabel.frame.size.width;
    }
   
    
    NSString *brand =[[NSString stringWithFormat:@"%@", [dict objectForKey:@"brand"]] replaceNullString];
    if (brand.length>0&&cat.length>0) {
        brand =[NSString stringWithFormat:@"  /  %@",brand];
    }else
    {
        brand =[NSString stringWithFormat:@"%@",brand];
    }
    if (brand.length>0) {
        [_subLabel setText:brand];
        CGSize optimumSize3 = [_subLabel optimumSize];
        _subLabel.frame =CGRectMake(x,_titleLabel.frame.origin.y+_titleLabel.frame.size.height+7,optimumSize3.width, optimumSize3.height);
    }
    
    [self layoutSubviews];
}
/// 返回Cell高度
+ (CGFloat)returnCellHeight:(id)model
{
    NSDictionary *dict =(NSDictionary*)model;
    
    CGFloat h =0;
    NSString *name =[[NSString stringWithFormat:@"%@", [dict objectForKey:@"title"]] replaceNullString];
    if (name.length>0) {
        name =[NSString stringWithFormat:@"%@",name];
        RTLabel *titleLabel =[[RTLabel alloc] initWithFrame:CGRectMake(PHOTO_FRAME_WIDTH,0, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*2,0)];
        titleLabel.backgroundColor = [UIColor clearColor];
        titleLabel.textAlignment = NSTextAlignmentLeft;
        titleLabel.font =FONT(20);
        titleLabel.lineSpacing =LSpacing;
        [titleLabel setText:name];
       CGSize optimumSize1 = [titleLabel optimumSize];
        h += optimumSize1.height;
        titleLabel=nil;
    }
    
    NSString *cat =[[NSString stringWithFormat:@"%@", [dict objectForKey:@"cat2"]] replaceNullString];
    NSString *brand =[[NSString stringWithFormat:@"%@", [dict objectForKey:@"brand"]] replaceNullString];
    if (cat.length>0|| brand.length>0) {

        cat =[NSString stringWithFormat:@"%@ / %@",cat,brand];
        RTLabel *titleLabel =[[RTLabel alloc] initWithFrame:CGRectMake(PHOTO_FRAME_WIDTH,0, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*2,0)];
        titleLabel.backgroundColor = [UIColor clearColor];
        titleLabel.textAlignment = NSTextAlignmentLeft;
        titleLabel.font =FONT(16);
        titleLabel.lineSpacing =LSpacing;
        [titleLabel setText:cat];
        CGSize optimumSize3 = [titleLabel optimumSize];
        h+=optimumSize3.height;
        titleLabel=nil;
    }
    return h+PHOTO_FRAME_WIDTH*2+PHOTO_TIME_PADDING;
}
-(void)handleSingleTapFromCategory
{
    if (self.actionBlock) {
        self.actionBlock(BEventType_category,self,nil,nil,self.indexPath);
    }
}
-(void)handleSingleTapFromBrand
{
    if (self.actionBlock) {
        self.actionBlock(BEventType_brand,self,nil,nil,self.indexPath);
    }
}
-(void)setCallBack:(void(^)(BEventType,UIView*,id,id,NSIndexPath *))callback
{
    self.actionBlock=callback;
}

@end
